package CustomUI;

import org.sqlStatements;

import com.example.dworld3.Draw;
import com.ydev.dworld3.R;

import GameTools.ydMath;
import GameTools.ydPoint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ydSeekBar extends View {
	
	int sID=-1;

	public int getsID() {
		return sID;
	}

	public void setsID(int sID) {
		this.sID = sID;
	}

	int defaultCoordinate = 0;

	int Type;
	int From = 0, To = 0;

	Path path1 = new Path();
	Paint paint1 = new Paint();

	Path cursorPath = new Path();
	Paint cursorPaint = new Paint();

	ydPoint curOrigin = new ydPoint();
	int cursorRadius = 100;
	
	Context mContext;
	
	ydPoint ydFrom=new ydPoint();
	ydPoint ydTo=new ydPoint();

	public ydSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		

		init(context, attrs);
	}

	public ydSeekBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.ydSeekBar);

		Type = a.getInteger(R.styleable.ydSeekBar_Type, 0);
		From = a.getInteger(R.styleable.ydSeekBar_From, 0);
		To = a.getInteger(R.styleable.ydSeekBar_To, 0);

		a.recycle();

		init(context, attrs);
	}

	public ydSeekBar(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	private void init(Context context, AttributeSet attrs) {
		mContext=context;
		
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.ydSeekBar);
	
		Type = a.getInteger(R.styleable.ydSeekBar_Type, 0);
		From = a.getInteger(R.styleable.ydSeekBar_From, 0);
		To = a.getInteger(R.styleable.ydSeekBar_To, 0);
	
		a.recycle();
		
		post(new Runnable() {
	
			@Override
			public void run() {
				paint1.setColor(Color.rgb(178, 178, 178));
				// paint1.setColor(Color.BLACK);
				paint1.setStrokeWidth(10);
				paint1.setStyle(Style.STROKE);
				path1.reset();
	
				cursorPaint.setColor(Color.BLUE);
				// path1.lineTo(200, 0);
				
				resetOrigin();
				invalidate();
	
			}
		});
	
	}

	public void resetOrigin() {
		
		if (Type == 1) {
			
			defaultCoordinate=getHeight()/2;
			path1.reset();
			path1.moveTo(defaultCoordinate, defaultCoordinate);
			
			path1.lineTo(getWidth(), defaultCoordinate);
	
			cursorPath.reset();
			cursorPath.moveTo(getWidth() / 2, defaultCoordinate);
			cursorPath.addCircle(getWidth() / 2, defaultCoordinate,
					cursorRadius, Direction.CCW);
			curOrigin.setCoordinates(getWidth() / 2, defaultCoordinate);
			ydFrom.setCoordinates(getWidth() / 2,defaultCoordinate);
	
		} else if (Type == 2) {
			
			defaultCoordinate=getWidth()/2;
			path1.reset();
			path1.moveTo(defaultCoordinate, defaultCoordinate);
			
			path1.lineTo(defaultCoordinate, getHeight());
	
			cursorPath.reset();
			cursorPath.moveTo(defaultCoordinate, getHeight() / 2);
			cursorPath.addCircle(defaultCoordinate, getHeight() / 2,
					cursorRadius, Direction.CCW);
			curOrigin.setCoordinates(defaultCoordinate, getHeight() / 2);
			
			ydFrom.setCoordinates(defaultCoordinate, getHeight() / 2);
		}
		else if (Type==3){
			defaultCoordinate=getWidth()/2;
			path1.reset();
			path1.addCircle(defaultCoordinate, defaultCoordinate, defaultCoordinate, Direction.CCW);
			
			cursorPath.reset();
			cursorPath.moveTo(defaultCoordinate, 0);
			cursorPath.addCircle(defaultCoordinate, 0,
					cursorRadius, Direction.CCW);
			curOrigin.setCoordinates(defaultCoordinate, 0);
		}
	
	}

	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.draw(canvas);
	//	canvas.drawColor(Color.RED);
	
		canvas.drawPath(cursorPath, cursorPaint);
		canvas.drawPath(path1, paint1);
		/*
		 * canvas.drawText(Type + "", 10, 100, paint1); canvas.drawText(From +
		 * "", 10, 300, paint1); canvas.drawText(To + "", 10, 400, paint1);
		 */

	}

	Boolean touchedCursor = false;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (x > curOrigin.x - cursorRadius / 2
					&& x < curOrigin.x + cursorRadius / 2
					&& y > curOrigin.y - cursorRadius / 2
					&& y < curOrigin.y + cursorRadius / 2) {
				touchedCursor = true;
			}
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			if (touchedCursor) {
				ydTo.setCoordinates(ydFrom);
				ydFrom.setCoordinates(x, y);
				curOrigin.setCoordinates(x, y);
				reDrawPath();
				movePath(x,y-getHeight());
			}

		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			touchedCursor=false;
			resetOrigin();
		}
		invalidate();
		return true;
	}

	private void movePath(float x, float y) {
		int moved=0;
		int from = 0, to = 0;
		if(Type==1){
			int dim=getWidth();
			float num=((float)(To-From)/dim);
			float dem=(x-dim/2);
			moved=(int) (num*dem);
			from=(int) ((ydFrom.x/dim)*(To-From));
			to=(int) ((ydTo.x/dim)*(To-From));
		}
		else if(Type==2){
			int dim=getHeight();
			float num=((float)(To-From)/dim);
			float dem=(y-dim/2);
			moved=(int) (num*dem);
			from=(int) ((ydFrom.y/dim)*(To-From));
			to=(int) ((ydTo.y/dim)*(To-From));
			//moved=(int) (((To-From)/getHeight())*(y-getHeight()/2));
		}
		else if(Type==3){
			to=(int) ydMath.vectorToDegrees(new ydPoint(ydTo.x-defaultCoordinate, ydTo.y-defaultCoordinate));
			from=(int) ydMath.vectorToDegrees(new ydPoint(ydFrom.x-defaultCoordinate, ydFrom.y-defaultCoordinate));
		}
		
		if(sID==1){
			((Draw)mContext).movePath(Type,from, to);
		}
		else if(sID==2){
			((Draw)mContext).transformLayer(Type,from, to);
		}
	}

	private void reDrawPath() {
		if (Type == 1) {

			cursorPath.reset();
			cursorPath.moveTo(curOrigin.x, defaultCoordinate);
			cursorPath.addCircle(curOrigin.x, defaultCoordinate, cursorRadius,
					Direction.CCW);
			// curOrigin.setCoordinates(getWidth()/2,defaultCoordinate);

		} else if (Type == 2) {

			cursorPath.reset();
			cursorPath.moveTo(defaultCoordinate, curOrigin.y);
			cursorPath.addCircle(defaultCoordinate, curOrigin.y, cursorRadius,
					Direction.CCW);
			// curOrigin.setCoordinates(defaultCoordinate,getHeight()/2);
		}
		else if (Type == 3) {
			
			/*double m=(curOrigin.y-defaultCoordinate)/(curOrigin.x-defaultCoordinate);
			double A=(Math.pow(m, 2)+1);
			double B=-2*defaultCoordinate;
			double C=0;//(Math.pow(defaultCoordinate, 2)-defaultCoordinate
			
			double x1=(-1*B+Math.sqrt(Math.pow(B, 2)-4*A*C))/(2*A);
			double x2=(-1*B-Math.sqrt(Math.pow(B, 2)-4*A*C))/(2*A);
			
			double y1=m*x1+defaultCoordinate;
			double y2=m*x2+defaultCoordinate;
			
			double xp=0;
			double xn=0;
			
			double ypp=0;
			double ynp=0;
			
			if(x1>0){
				xp=x1;
				ypp=y1;
				
				xn=x2;
				ynp=y2;
			}
			else{
				xp=x2;
				ypp=y2;
				
				xn=x1;
				ynp=y1;
			}*/
			
			double hyp=ydMath.hypotenuse(curOrigin, new ydPoint(defaultCoordinate, defaultCoordinate));
			
			float xt=(float) ((curOrigin.x-defaultCoordinate)/hyp)*defaultCoordinate;
			xt+=defaultCoordinate;
			
			float yt=(float) ((curOrigin.y-defaultCoordinate)/hyp)*defaultCoordinate;
			yt+=defaultCoordinate;
			
			/*path1.reset();
			path1.addCircle(defaultCoordinate, defaultCoordinate, defaultCoordinate, Direction.CCW);
			path1.moveTo(defaultCoordinate, defaultCoordinate);
			path1.lineTo(xt, yt);*/
			
		/*	if(curOrigin.x>defaultCoordinate){
				curOrigin.setCoordinates((float)xp, (float)ypp);
			}
			else if(curOrigin.x<defaultCoordinate){
				curOrigin.setCoordinates((float)xn, (float)ynp);
			}*/
			curOrigin.setCoordinates(xt, yt);
			
			
			cursorPath.reset();
			cursorPath.moveTo(defaultCoordinate, curOrigin.y);
			cursorPath.addCircle(curOrigin.x, curOrigin.y, cursorRadius,
					Direction.CCW);
			// curOrigin.setCoordinates(defaultCoordinate,getHeight()/2);
		}
		
	}

}
